其他
这些屌毛总是给订阅号发黄x图,连夜肝了一个微信订阅号鉴黄机器人抵御~
来源公众号:码匠笔记
2、开发服务端,并验证服务器地址的有效性
3、处理具体的业务逻辑
# 配置微信公众号
开发者ID,开发者调用的唯一标示,调用接口的时候需要传递。
开发者密码,这个很重要一定要保存在自己的服务器上面,用于验证安全性。
服务地址,这个就是我们用来接收微信平台转发的用户消息的服务的地址
令牌,用户接收信息时候做验证是否请求来自微信平台
用于加密消息,防止被截获,如果 6 设置为明文模式不需要这个配置。
是否加密传输消息
是我们具体的服务器地址,path是 weixin/receive 这个下文中具体代码部分会详细讲解
Token 随便生成一个 UUID 就可以
随机生成,后面如果调用 API 会用到。
# 编写服务端
<repositories>
<repository>
<id>developer-weapons-repository</id>
<url>https://raw.githubusercontent.com/developer-weapons/repository/master</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.developer.weapons</groupId>
<artifactId>wechat-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
@Autowired
private WechatOfficialService wechatOfficialService;
@Value("${weixin.token}")
private String token;
@RequestMapping(value = "/weixin/receive", method = RequestMethod.GET)
public void receive(
@RequestParam(value = "signature") String signature,
@RequestParam(value = "timestamp") String timestamp,
@RequestParam(value = "nonce") String nonce,
@RequestParam(value = "echostr") String echostr,
HttpServletResponse response) throws IOException {
boolean valid = wechatOfficialService.isValid(signature, timestamp, nonce, token);
PrintWriter writer = response.getWriter();
if (valid) {
writer.print(echostr);
} else {
writer.print("error");
}
writer.flush();
writer.close();
}
# 处理业务逻辑
@RequestMapping(value = "/weixin/receive", method = RequestMethod.POST)
public void receive(
@RequestParam(value = "signature") String signature,
@RequestParam(value = "timestamp") String timestamp,
@RequestParam(value = "nonce") String nonce,
HttpServletRequest request,
HttpServletResponse response) throws IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
boolean valid = wechatOfficialService.isValid(signature, timestamp, nonce, token);
PrintWriter writer = response.getWriter();
if (!valid) {
writer.print("error");
writer.flush();
writer.close();
return;
}
try {
Map<String, String> map = wechatOfficialService.toMap(request.getInputStream());
if (map.get("MsgType").equals("image")) {
String msg = OfficialAutoReplyMessage.build()
.withContent("接收到图片链接为:" + map.get("PicUrl"))
.withMsgtype(MessageTypeEnum.TEXT)
.withFromUserName(map.get("ToUserName"))
.withToUserName(map.get("FromUserName"))
.toXml();
writer.print(msg);
writer.flush();
writer.close();
return;
}
} catch (Exception e) {
log.error("WeixinController receive error", e);
}
writer.print("success");
writer.flush();
writer.close();
}
if (map.get("MsgType").equals("image")) {
String res = checkService.check(publicKey, privateKey, map.get("PicUrl"));
OfficialAutoReplyMessage officialAutoReplyMessage =
OfficialAutoReplyMessage.build()
.withMsgtype(MessageTypeEnum.TEXT)
.withFromUserName(map.get("ToUserName"))
.withToUserName(map.get("FromUserName"));
if (StringUtils.equals("forbid", res)) {
officialAutoReplyMessage.withContent("小哥,你的图片有点问题哦");
} else {
officialAutoReplyMessage.withContent("骚年,你这图片刚刚的没问题");
}
writer.print(officialAutoReplyMessage.toXml());
writer.flush();
writer.close();
return;
}
完整源码获取方法,老规矩啦!
识别下方二维码,关注后回复【1129】
即可获取机器人完整源码
👆长按上方二维码 2 秒